<?php
/**
* @Copyright Copyright (C) 2010- ... Andy Sharman
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class customFields
{
	
	private $dbo;
	
	function customFields()
	{
		$this->dbo =& JFactory::getDBO();
	}	
	
	function countFields()
	{
		$sql = 'SELECT `id`
				FROM #__cf_fields';
		$this->dbo->setQuery($sql);
		$this->dbo->Query();
		$count = count($this->dbo->loadObjectList());
		
		return $count;
	}
	
	function addField($name, $position)
	{
		if (!empty($name) && !empty($position))
		{
			$sql = 'INSERT INTO #__cf_fields (`name`,`position`) VALUES ("'.mysql_real_escape_string($name).'","'.$position.'")';
			$this->dbo->setQuery($sql);
			$this->dbo->Query();		
			
			echo '<h2 class="succes">Custom field succesfully added.</h2>';
		}
		else
		{
			echo '<h2 class="error">Please make sure you have entered a name and position for the new field.</h2>';
		}
	}
	
	function listFields()
	{
		$sql = 'SELECT id,name,position
				FROM #__cf_fields
				ORDER BY id DESC';
		$this->dbo->setQuery($sql);
		$this->dbo->Query();
		$list = $this->dbo->loadObjectList();
		
		return $list;				
	}
	
	function updateField($id,$newName,$newPosition)
	{
		if (!empty($newName) && !empty($newPosition))
		{
			$sql = 'UPDATE #__cf_fields SET `name`="'.mysql_real_escape_string($newName).'", `position`="'.$newPosition.'" WHERE `id`='.$id;
			$this->dbo->setQuery($sql);
			$this->dbo->Query();
			
			echo '<h2 class="succes">Custom field succesfully updated.</h2>';
		}
		else
		{
			echo '<h2 class="error">Please make sure you have entered a name and position for the field update.</h2>';
		}
	}
	
	function deleteField($id)
	{
		$sql = 'DELETE FROM #__cf_fields WHERE `id`='.$id;
		$this->dbo->setQuery($sql);
		$this->dbo->Query();
		echo '<h2 class="succes">Custom field succesfully deleted.</h2>';
	}
	
	function countValues($article_id)
	{
		$sql = 'SELECT `id`
				FROM #__cf_values
				WHERE `article_id`='.$article_id;
		$this->dbo->setQuery($sql);
		$this->dbo->Query();
		$count = $this->dbo->getNumRows();
		
		return $count;
	}
	
	function addValue($field_id, $article_id, $value)
	{
		if (!empty($value))
		{
			$sql = 'INSERT INTO #__cf_values (`field_id`,`article_id`,`value`) VALUES ("'.$field_id.'","'.$article_id.'","'.mysql_real_escape_string($value).'")';
			$this->dbo->setQuery($sql);
			$this->dbo->Query();
			
			echo '<h2 class="succes">Custom field succesfully added.</h2>';
		}
		else
		{
			echo '<h2 class="error">Please make sure you have entered a name and position for the new field.</h2>';
		}
	}
	
	function listValues($article_id)
	{
		$sql = 'SELECT id,field_id,value
				FROM #__cf_values
				WHERE `article_id`='.$article_id.'
				ORDER BY id DESC';
		$this->dbo->setQuery($sql);
		$this->dbo->Query();
		$list = $this->dbo->loadObjectList();
		
		return $list;				
	}
	
	function updateValue($id,$newField_id,$newValue)
	{
		if (!empty($newField_id) && !empty($newValue))
		{
			$sql = 'UPDATE #__cf_values SET `field_id`="'.$newField_id.'", `value`="'.mysql_real_escape_string($newValue).'" WHERE `id`='.$id;
			$this->dbo->setQuery($sql);
			$this->dbo->Query();
			
			echo '<h2 class="succes">Custom field value succesfully updated.</h2>';
		}
		else
		{
			echo '<h2 class="error">Please make sure you have entered a value and field name for the value update.</h2>';
		}
	}
	
	function deleteValue($id)
	{
		$sql = 'DELETE FROM #__cf_values WHERE `id`='.$id;
		$this->dbo->setQuery($sql);
		$this->dbo->Query();
		echo '<h2 class="succes">Custom field value succesfully deleted.</h2>';
	}
	
	/* Advanced */
	
	function updateOutput($fileContents)
	{
		if (!empty($fileContents))
		{

			$fileContents = html_entity_decode(stripslashes($fileContents),ENT_QUOTES);
			
			if (file_put_contents(JPATH_COMPONENT.DS.'cf.output.php',$fileContents))
			{
				echo '<h2 class="success">Output has been changed!</h2>';
			}
			else
			{
				echo '<h2 class="error">There is an error writing to file cf.output.php, please make sure the file exists and permissions are set correctly.</h2>';	
			}
		}
		else
		{
			echo '<h2 class="error">You need to enter something into the output to continue.</h2>';
		}
	}
	
}